package com.extension.company.common.mvp;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.utils.DisplayUtils;
import com.jaeger.library.StatusBarUtil;

import butterknife.ButterKnife;

public abstract class BaseTitleActivity<V extends IView, P extends BasePresenter> extends AppCompatActivity {

    protected P presenter;

    private TextView m_title; // 标题

    private ImageView m_back;   // 返回

    private TextView m_right;

    private LinearLayout view_title; //标题布局

    private LinearLayout ll_view_main; // 内容部分位置

    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置键盘默认隐藏
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        setContentView(R.layout.base_activity);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //sdk大于或等于11
            //启动硬件加速
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE
                    |View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }else {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.black2));
        }
        //创建Presenter
        presenter = createPresenter();
        if (presenter != null) {
            //将View层注册到Presenter中
            presenter.registerView(createView());
            //将Mode层注册到presenter
            presenter.registerModel();
        }
        initBaseView();
        ButterKnife.bind(this);
        initData();
        initListener();
    }

    private void initBaseView() {
        inflater = getLayoutInflater();

        ll_view_main = findViewById(R.id.ll_view_main);
        // 加载内容视图
        LinearLayout.LayoutParams params_main = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        //添加内容视图
        ll_view_main.addView(inflater.inflate(getMainLayout(), null), params_main);

        //添加标题视图
        view_title = findViewById(R.id.view_title);
        LinearLayout.LayoutParams params_title = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                DisplayUtils.convertDip2Px(this, 44));
        view_title.addView(inflater.inflate(R.layout.base_header_title, null), params_title);

        m_title = findViewById(R.id.m_title);
        m_back = findViewById(R.id.m_back);
        m_right = findViewById(R.id.m_right);
        m_back.setOnClickListener(mOnClick);
        m_right.setOnClickListener(mOnClick);
    }

    protected abstract void initData();
    protected abstract void initListener();
    /**
     * 获得主界面layout资源
     *
     * @return
     */
    public abstract int getMainLayout();

    public abstract V createView();

    public abstract P createPresenter();

    public void setTitle(String title) {
        m_title.setText(title);
    }

    // 监听
    private View.OnClickListener mOnClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.m_back:
                    //返回
                    onBtnCancel();
                    break;
                case R.id.m_right:
                    //返回
                    action_Right(v);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1) {
            //非默认值
            getResources();
        }
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
    /**
     * 设置是否显示右键
     *
     * @param show
     */
    public void setShowRight(boolean show) {
        if (show) {
            m_right.setVisibility(View.VISIBLE);
        } else {
            m_right.setVisibility(View.GONE);
        }
    }

    /**
     * 标题栏右键1文本
     *
     * @param str
     */
    public void setTvRight(String str) {
        m_right.setText(str);
    }

    /**
     * 标题栏右键1按钮事件
     *
     * @param v
     */
    public void action_Right(View v) {
    }

    /**
     * 取消方法
     */
    public void onBtnCancel() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            onBtnCancel();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            //Activity销毁时的调用，让具体实现BasePresenter中onViewDestroy()方法做出决定
            presenter.destroy();
        }
    }
}
